package com.jxl.domain.service.impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jxl.common.auth.Roles;
import com.jxl.common.enums.DeletedCodeEnum;
import com.jxl.common.redis.RedisUtil;
import com.jxl.domain.convert.AuthPermissionBOConvert;
import com.jxl.domain.convert.AuthUserBOConvert;
import com.jxl.domain.entity.AuthPermissionBO;
import com.jxl.domain.entity.AuthUserBO;
import com.jxl.domain.entity.AuthUserRoleBO;
import com.jxl.domain.service.AuthPermissionDomainService;
import com.jxl.domain.service.AuthUserDomainService;
import com.jxl.domain.service.AuthUserRoleDomainService;
import com.jxl.infra.basic.entity.AuthPermission;
import com.jxl.infra.basic.entity.AuthRole;
import com.jxl.infra.basic.entity.AuthUser;
import com.jxl.infra.basic.service.AuthPermissionService;
import com.jxl.infra.basic.service.AuthRoleService;
import com.jxl.infra.basic.service.AuthUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class AuthPermissionDomainServiceImpl implements AuthPermissionDomainService {

    @Resource
    private AuthPermissionService authPermissionService;

    @Resource
    private RedisUtil redisUtil;

    private String authPermissionPrefix = "auth.permission";


    @Override
    public Boolean add(AuthPermissionBO authPermissionBO) {
        AuthPermission authPermission =
                AuthPermissionBOConvert.INSTANCE.convertBOToAuthPermission(authPermissionBO);
        int insert = authPermissionService.insert(authPermission);
        return insert>0;
    }

    @Override
    public Boolean update(AuthPermissionBO authPermissionBO) {
        AuthPermission authPermission =
                AuthPermissionBOConvert.INSTANCE.convertBOToAuthPermission(authPermissionBO);
        int update = authPermissionService.update(authPermission);
        return update>0;
    }

    @Override
    public Boolean delete(AuthPermissionBO authPermissionBO) {
        AuthPermission authPermission =
                AuthPermissionBOConvert.INSTANCE.convertBOToAuthPermission(authPermissionBO);
        authPermission.setIsDeleted(DeletedCodeEnum.DELETE.getCode());
        int delete = authPermissionService.update(authPermission);
        return delete>0;
    }

    @Override
    public List<String> getPermission(String userName) {
        //构建key
        String key = redisUtil.buildKey(authPermissionPrefix, userName);
        String value = redisUtil.get(key);
        if (value==null) return Collections.emptyList();
        List<AuthPermission> permissionList = new Gson().fromJson(value, new TypeToken<List<AuthPermission>>() {
        }.getType());
        List<String> collect = permissionList.stream().map(AuthPermission::getPermissionKey).collect(Collectors.toList());
        return collect;
    }
}
